package com.tang.componet.mvc.interceptor;

import com.tang.common.context.LoginContextHolder;
import com.tang.common.enums.HeaderAndContextNameEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader(HeaderAndContextNameEnum.LOGIN_ID.getName());
        if (StringUtils.isNotBlank(header)) {
            LoginContextHolder.set(HeaderAndContextNameEnum.LOGIN_ID.getName(), header);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LoginContextHolder.remove();
    }
}
